#include "GY_SimpleGroup.h"
#include <QDebug>

GY_SimpleGroup::GY_SimpleGroup(const QString &guid, QGraphicsItem *parent) : GY_ContainerTransform(guid,this, parent)
{
    initializationPropertyInfo();
    this->setCenterPointF(m_PropertyInfoMap["Position"].propertyValue.toPointF());
}

void GY_SimpleGroup::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update|OrdinaryAttributes;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SimpleGroup::updateObjectName;
    m_PropertyInfoMap.insert("ObjectName",ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    //位置
    GY_PropertyInfo positonPropertyInfo(this);
    positonPropertyInfo.propertyName="Position";
    positonPropertyInfo.propertyValue=QPointF(0,0);
    positonPropertyInfo.updateType = Update|MoveUpdate|PointOfRelative;
    positonPropertyInfo.propertyType = PointPropertyType;
    positonPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_SimpleGroup::updatePosition;
    m_PropertyInfoMap.insert(positonPropertyInfo.propertyName,positonPropertyInfo);
    m_PropertyInfoMap[positonPropertyInfo.propertyName].setConnect();

    //隐藏
    GY_PropertyInfo isVisiblePropertyInfo(this);
    isVisiblePropertyInfo.propertyName="IsVisible";
    isVisiblePropertyInfo.propertyValue=true;
    isVisiblePropertyInfo.updateType = Update|OrdinaryAttributes;
    isVisiblePropertyInfo.propertyType = BoolPropertyType;
    isVisiblePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_SimpleGroup::updateIsVisible;
    m_PropertyInfoMap.insert(isVisiblePropertyInfo.propertyName,isVisiblePropertyInfo);
    m_PropertyInfoMap[isVisiblePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo enablePropertyInfo(this);
    enablePropertyInfo.propertyName="Enable";
    enablePropertyInfo.propertyValue=true;
    enablePropertyInfo.updateType = Update|OrdinaryAttributes;
    enablePropertyInfo.propertyType = BoolPropertyType;
    enablePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_SimpleGroup::updateEnable;
    m_PropertyInfoMap.insert(enablePropertyInfo.propertyName,enablePropertyInfo);
    m_PropertyInfoMap[enablePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo updateChildrenOnlyIfVisiblePropertyInfo(this);
    updateChildrenOnlyIfVisiblePropertyInfo.propertyName="UpdateChildrenOnlyIfVisible";
    updateChildrenOnlyIfVisiblePropertyInfo.propertyValue=false;
    updateChildrenOnlyIfVisiblePropertyInfo.updateType = Update|OrdinaryAttributes;
    updateChildrenOnlyIfVisiblePropertyInfo.propertyType = BoolPropertyType;
    updateChildrenOnlyIfVisiblePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_SimpleGroup::updateUpdateChildrenOnlyIfVisible;
    m_PropertyInfoMap.insert(updateChildrenOnlyIfVisiblePropertyInfo.propertyName,updateChildrenOnlyIfVisiblePropertyInfo);
    m_PropertyInfoMap[updateChildrenOnlyIfVisiblePropertyInfo.propertyName].setConnect();

}

bool GY_SimpleGroup::updatePosition(const QString &name, const QVariant &value)
{
//    QString str = this->getObjName();
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    //setpos设置为父级坐标
    QPointF point = value.toPointF();
    if(this->parentItem()) {
        point = this->mapToParent(this->mapFromScene(value.toPointF()));
    }
    this->setPos(point);
    m_PropertyInfoMap[name].propertyValue=this->mapFromScene(value.toPointF());
    return true;
}

bool GY_SimpleGroup::updateIsVisible(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    this->setVisible(value.toBool());
    return true;
}

bool GY_SimpleGroup::updateEnable(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    return true;
}

bool GY_SimpleGroup::updateUpdateChildrenOnlyIfVisible(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    return true;
}

